#!/bin/bash
[% c("var/set_default_env") -%]
[% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
distdir=/var/tmp/dist/[% project %]
mkdir -p $distdir
mkdir -p /var/tmp/build

[% IF c("var/android") %]
  # We need to make sure our ZSTD dependencey is properly picked up.
  # --enable-zstd is not enough.
  export ZSTD_CFLAGS=-I/var/tmp/dist/zstd/include
  export ZSTD_LIBS=/var/tmp/dist/zstd/lib/libzstd.a
[% END %]

TORDATADIR="$distdir/data"
TORBINDIR="$distdir/tor"

mkdir $TORDATADIR
mkdir $TORBINDIR

[% IF c("var/windows") || c("var/android") %]
  tar -C /var/tmp/dist -xf [% c('input_files_by_name/zlib') %]
  zlibdir=/var/tmp/dist/zlib
[% END %]
[% IF c("var/android") %]
  tar -C /var/tmp/dist -xf [% c('input_files_by_name/zstd') %]
[% END %]
tar -C /var/tmp/dist -xf [% c('input_files_by_name/openssl') %]
tar -C /var/tmp/dist -xf [% c('input_files_by_name/libevent') %]
tar -C /var/tmp/build -xf [% project %]-[% c('version') %].tar.gz
libeventdir=/var/tmp/dist/libevent
openssldir=/var/tmp/dist/openssl/openssl

[% IF c("var/linux") %]
  TORDEBUGDIR="$distdir/debug"
  mkdir "$TORDEBUGDIR"

  cp $openssldir/lib/libssl.so.1.1 "$TORBINDIR"
  cp $openssldir/lib/libcrypto.so.1.1 "$TORBINDIR"
  cp $libeventdir/lib/libevent-2.1.so.7 "$TORBINDIR"
  # We need to copy the libstdc++.so.6 for Tor Browser on older Linux distros.
  # Copying it into /Browser, which feels more natural, and amending
  # LD_LIBRARY_PATH breaks updates from a Tor Browser with the old
  # LD_LIBRARY_PATH value to the Tor Browser with the newer one. Thus, we copy
  # the libstdc++ into the directory with the libs tor depends on, too. See bug
  # 13359 for further details.
  cp /var/tmp/dist/gcc/[% c("var/libdir") %]/libstdc++.so.6 "$TORBINDIR"
  [% IF c("var/asan") -%]
    cp /var/tmp/dist/gcc/[% c("var/libdir") %]/libasan.so.6 "$TORBINDIR"
    cp /var/tmp/dist/gcc/[% c("var/libdir") %]/libubsan.so.1 "$TORBINDIR"
  [% END -%]
  chmod 700 "$TORBINDIR"/*.so*
  # This is needed to make RPATH unavailable. See bug 9150.
  export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$TORBINDIR"
[% END %]

[% IF c("var/macos") %]
  cp $libeventdir/lib/libevent-*.dylib "$TORBINDIR"
[% END %]

[% IF c("var/windows") %]
  export CC=[% c("arch") %]-w64-mingw32-cc
[% END %]

cd /var/tmp/build/[% project %]-[% c('version') %]

[% IF c("var/android") -%]
# Remove reference to OpenSSL 1.1.1b. See https://gitlab.torproject.org/tpo/core/tor/-/issues/40759
  sed -i 's/ A fix is expected to appear in OpenSSL //g' src/lib/tls/tortls_openssl.c
  sed -i 's/1.1.1b.//g' src/lib/tls/tortls_openssl.c
[% END -%]

# add git hash to micro-revision.i for #24995
echo '"[% c("abbrev", { abbrev_length => 16 }) %]"' > micro-revision.i
./autogen.sh
find -type f -print0 | xargs -0 [% c("touch") %]
./configure --disable-asciidoc --with-libevent-dir="$libeventdir" --with-openssl-dir="$openssldir" \
    [% IF c("var/asan") %]--enable-fragile-hardening[% END %] \
    [% IF c("var/windows") || c("var/android") %]--with-zlib-dir="$zlibdir"[% END %] \
    [% IF c("var/macos") %]--enable-static-openssl[% END %] \
    [% IF c("var/windows") %]--enable-static-libevent --enable-static-openssl --enable-static-zlib[% END %] \
    --prefix="$distdir" [% c("var/configure_opt") %]
[% IF c("var/macos") -%]
  export LD_PRELOAD=[% c("var/faketime_path") %]
  export FAKETIME="[% USE date; GET date.format(c('timestamp'), format = '%Y-%m-%d %H:%M:%S') %]"
[% END -%]
make -j[% c("num_procs") %]
make install
[% IF c("var/macos") -%]
  unset LD_PRELOAD
[% END -%]

cp $distdir/share/tor/geoip "$TORDATADIR"
cp $distdir/share/tor/geoip6 "$TORDATADIR"

[% IF c("var/macos") %]
  LIBEVENT_FILE=`basename $libeventdir/lib/libevent-*.dylib`
  cd $distdir
  cp bin/tor "$TORBINDIR"
  cd "$TORBINDIR"
  [% c("var/build_target") %]-install_name_tool -change "$libeventdir/lib/$LIBEVENT_FILE" "@executable_path/$LIBEVENT_FILE" tor
[% END %]

cd $distdir
[% IF c("var/windows") %]
  install -s $distdir/bin/tor.exe "$TORBINDIR"
  install -s $distdir/bin/tor-gencert.exe "$TORBINDIR"
[% END %]

[% IF c("var/linux") %]
  # Strip and generate debuginfo for libs
  objcopy --only-keep-debug $distdir/bin/tor "$TORDEBUGDIR/tor"
  install -s $distdir/bin/tor "$TORBINDIR"
  objcopy --add-gnu-debuglink="$TORDEBUGDIR/tor" "$TORBINDIR/tor"
  for i in "$TORBINDIR"/*so*
  do
      LIB=`basename $i`

      if [ $LIB == 'libstdc++.so.6' ]; then
        # keeping this separate to maintain reproducibility; we can probably
        # treat this the same as the rest (though it seems libstdc++ doesn't come with
        # any useful debug symbols since we don't build it, so maybe we should figure
        # out how to package them
        strip "$TORBINDIR/$LIB"
      else
        objcopy --only-keep-debug "$TORBINDIR/$LIB" "$TORDEBUGDIR/$LIB"
        strip "$TORBINDIR/$LIB"
        objcopy --add-gnu-debuglink="$TORDEBUGDIR/$LIB" "$TORBINDIR/$LIB"
      fi
  done
[% END %]

[% IF c("var/android") %]
  install -s --strip-program=[% c("var/cross_prefix") %]-strip "$distdir/bin/tor" "$TORBINDIR/libTor.so"
[% END %]

[%
  SET tar_src = [ "tor", "data" ];
  IF c("var/linux");
   tar_src.push("debug");
  END;
  c('tar', {
    tar_src => tar_src,
    tar_args => '-czf ' _ dest_dir _ '/' _ c('filename'),
  });
%]
